App.controller("albumManagementCtrl",function($scope, $rootScope, $location, $http, $compile, path, publicService) {
	$scope.search1 = {};	//专辑搜索表单
	$scope.search2 = {};	//文章搜索表单
	$scope.AlbumShows = true; //专辑列表、专辑设置的显示与隐藏
	$scope.belongSpecial = true;  //所属专辑 的显示与隐藏
	$scope.album = {};			//专辑
	$scope.article = {};		//图文
	var albumInfo = null;	//专辑信息
	var articleInfo = null;	//文章信息
	var recordUser_type = null;
	var specialListUrl = null;//专辑列表
	var articleListUrl= null;//文章列表
	
	var limitParam1 = {imageSize:80, format:"jpg"};		//头图格式
	var limitParam2 = {format:"jpg"};		//缩略图格式
	var limitParam3 = {imageSize:150, format:"jpg"};	//专栏图格式
	
	var editor = UE.getEditor('editor');
	if(editor.container!=undefined){
		editor.destroy();
		editor = UE.getEditor('editor');
	}

	
	//文章列表
	$scope.mainGridOptions2 = {
			dataSource : {
				transport : {
					read : {
						url :path+"/server/querynews",
						type : 'POST',
						dataType : "json",
						contentType : "application/json"
					},
					parameterMap : function(options, operation) {
						if (operation == "read") {
							var parameter = {
								page : options.page, //当前页
								pageSize : options.pageSize,
								param:{
									
								}
							};
							console.log('查询参数',parameter);
							return kendo.stringify(parameter);
						}
					},
				},
				pageSize : 10,
				serverPaging : true,
				serverSorting: true,
				schema : {
					data : function(d) {
						return d.result.data;
					},
					total : function(d) {
						return d.result.total; //总条数
					}
				},
			},
			pageable : {  //分页
				pageSizes: [10, 20, 50, 100],  //每页显示记录数
			  },   
			sortable: {   //排序
				//allowUnsort: false,  //允许无序
			    mode: "single"     //排序模式：single，multiple
		    },
			editable: false,  //true：单个编辑；popup：弹出新页面编辑
			dataBound: function () {   //序号
		        var rows = this.items();
		        var page = this.pager.page() - 1;
		        var pagesize = this.pager.pageSize();
		        $(rows).each(function () {
		            var index = $(this).index() + 1 + page * pagesize;
		            var rowLabel = $(this).find(".row-number");
		            $(rowLabel).html(index);
		        });
		    },
			columns : [
					{    
						title : "序号",
						field : "article_id", 
						width : "50px",
						template:"<span class='row-number subTitle'></span>"					
					},
					{
						field : "article_title",         
						title : "文章标题",     
						width : "120px"    
					},
					{
						field : "article_createTime1",         
						title : "创建日期",     
						width : "120px",
						format: "{0: yyyy-MM-dd HH:mm:ss}"
					},
					{
						field : "id",         
						title : "操作",     
						width : "100px",
						template:function(e){
							var html = '';
							
							html += ('<div class="k-state-default operation splitButtona" ng-click="editArticle(\''+e.article_id+'\')">编辑</div>'
									+'<div class="k-state-default operation splitButtona" ng-click="deleteArticle(\''+e.article_id+'\')">删除</div>');
							
							return html;
						}
					}
			]
		};
	
	//查询
	$scope.search = function(type){
		if(type==1){
			$scope.grid1.dataSource.page(1);
		}else if(type==2){
			$scope.grid2.dataSource.page(1);
		}
	}

	
	//新增
	$scope.add = function(type){
		if(type==1){
			$scope.album = {};
			publicService.removeUploadImage("logo1_img","logo1_url");
			
			$scope.addAlbumWindow.center().open();   //打开弹框
		}else if(type==2){
			$scope.article = {};
			publicService.removeUploadImage("logo2_img","logo2_url");
			editor.setContent("");   	   //内容

			$scope.addArticleWindow.center().open();   //打开弹框
		}
	}
	
	//获取所有的专辑数据
	$scope.getAllAlbumData = function(containerId){
		$("#"+containerId).kendoDropDownList({
			dataTextField: "name",
		    dataValueField: "id",
		    dataSource: {
			      serverFiltering: true,
		          transport: {
		              read: {
		            	  type : 'POST',
		                  url: path+"/server/queryabumlist",
		                  dataType : "json"
		              }
		          },
		          schema : {
						data : function(data) {
							data.unshift({id:"",name:"请选择"});
							return data;
						}
				  }
	       }
		});
	}

	//【专辑】添加
	$scope.addNewAlbum = function(){
		$scope.album.top_img_url = $("#logo1_url").val();  //头图
		
		//验证
		if(!$scope.album.name){
			alert("请填写【专辑名称】！");
			return;
		}
		if(!$scope.album.top_img_url){
			alert("请上传【头图】！");
			return;
		}
		
		var url = '';
		if(userInfo.user_type==1){  //开发者
			if($scope.album && $scope.album.id){
				url = path+'/server/modfiyabum';
			}else{
				url = path+'/server/addabum';
			}
		}else if(userInfo.user_type==2){   //厂家
			if($scope.album && $scope.album.id){
				url = path+'/server/modfiyabum';
			}else{
				url = path+'/server/addabum';
			}
		}
		$http({
			   url: url,
			   method: 'POST',
			   data: angular.toJson($scope.album)
		}).success(function(data){
			if(data.code==0){  //成功
				$scope.addAlbumWindow.close();   //关闭
				$scope.grid1.dataSource.page(1);
			}else if(data.code==1){  //失败
				alert(data.message);
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//【专辑】编辑
	$scope.editAlbum = function(id){
		var url = '';
		if(userInfo.user_type==1){  //开发者
			 url: path+'/server/getabumbyid/'+id;
		}else if(userInfo.user_type==2){   //厂家
			url:path+'/server/removeabum/'+id;
		}
		$http({
			   url:url,
			   method: 'GET'
		}).success(function(data){
			if(data.code==0){  //成功
				albumInfo = data.result;

				$scope.album.id = albumInfo.id;
				$scope.album.name = albumInfo.name;			//专辑名称
				publicService.showUploadImage("logo1_img","logo1_url",albumInfo.top_img_url);			//文章标题
				
				$scope.addAlbumWindow.center().open();   //打开弹框
			}else if(data.code==1){  //失败
				alert(data.message);
			}
		}).error(function(data){
			alert(data);   
		})
	}
	

	
	//【文章】添加
	$scope.addNewArticle = function(){
		$scope.article.image = $("#logo2_url").val();  //缩略图
		$scope.article.details = editor.getContent();   //内容
		$scope.article.type = 1;    //文章类型（1：专辑，2：图文）
		$scope.article.state = 1;   //状态(0 过期 1 可用）

		//验证
		if(!$scope.article.title){
			alert("请填写【文章标题】！");
			return;
		}
		if(!$scope.article.digest){
			alert("请填写【文章概述】！");
			return;
		}
		if(!$scope.article.author){
			alert("请填写【文章作者】！");
			return;
		}
		if(!$scope.article.image){
			alert("请上传【缩略图】！");
			return;
		}
		if(!$scope.article.details){
			alert("请填写【图文详情】！");
			return;
		}
		
		var url = '';
		if($scope.article && $scope.article.id){  //编辑
			url = path+'/server/updateArticle';
		}else{//新增
			url = path+'/server/addArticle';
		}
		$http({
			   url: url,
			   method: 'POST',
			   data: angular.toJson($scope.article)
		}).success(function(data){
			if(data.code==0){  //成功
				$scope.addArticleWindow.close();   //关闭
				$scope.grid2.dataSource.page(1);
			}else if(data.code==1){  //失败
				alert(data.message);
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//【文章】编辑
	$scope.editArticle = function(id){

		$http({
			   url:path+'/server/queryarticledetails/'+id,
			   method: 'GET'
		}).success(function(data){
			if(data.code==0){  //成功
				articleInfo = data.result;
				$scope.article.author = articleInfo.author;   //作者
				$scope.article.id = articleInfo.id;
				$scope.article.title = articleInfo.title;			//文章标题
				$scope.article.digest = articleInfo.digest;			//文章概述
				publicService.showUploadImage("logo2_img","logo2_url",articleInfo.image);   //缩略图上传
				editor.setContent(articleInfo.details);   	   //内容
				$scope.article.state = 1;   //状态(0 过期 1 可用）
				
				$scope.addArticleWindow.center().open();   //打开弹框
			}else if(data.code==1){  //失败
				alert(data.message);
			}
		}).error(function(data){
			alert(data);   
		})
	}
	
	//【文章】删除
	$scope.deleteArticle = function(id){
		if(confirm("确认删除本条图文？")){
			$http({
				   url: path+'/server/deleteArticleByArticleId/'+id,
				   method: 'GET'
			}).success(function(data){
				if(data.code==0){  //成功
					$scope.grid2.dataSource.page(1);
				}else if(data.code==1){  //失败
					alert(data.message);
				}
			}).error(function(data){
				alert(data);   
			})
		}
	}


	
	
	publicService.initDate("publish_time1");
	publicService.uploadImage("logo1","logo1_img","logo1_url",limitParam1);		//头图
	
	publicService.uploadImage("logo2","logo2_img","logo2_url",limitParam2);		//缩略图
	
	
	


});